var data_all;//用全局变量存储当前页面后端返回信息
var record_page = '';//recore_page代表当前所处页数
var limit = 8;//页数上限

$('#main_content').ready(function () {
            //var id = localStorage.getItem("user_id");
            //var json = {
            //        "token": localStorage.getItem("token"),
            //        "user_id": localStorage.getItem("userid"),
            //        "start": 0,
            //       "range": 10
            //    };
            $.ajax({
                url:"/club_course/showCourseList",
                type:"GET",
                datatype:'json',
                contentType: 'application/json',
            //    data: JSON.stringify(json),
                success: function(data)
                {
                    code = data['success'];
                    if(code == true)
                    {
                        alert("读取信息出错！");
                    }
                    else
                    {
                        data_all = data;//存入后端信息
                        if(window.location.href.split("=")[1])
                        {
                            record_page = window.location.href.split("=")[1];//根据页面的url中的参数修改页数
                        }
                        else
                        {
                            record_page = 1;//默认置为第一页
                        }
                        //payload = data['data'];
                        //result = payload['raws'];
                        //for(var i = 0; i < payload['record']; i++ )
                        for( var i = record_page; i < 7; i++ )
                        {
                            //each = result[i];
                            //$("#main_content").append('<div class="panel panel-default">' + '<div class="panel-body">' + '<div class="panel-p">' +'<div class="panel-fp-l">' + each['name'] + '</div>' + '<div class="panel-fp-r">' + each['count'] + '人学过' + '</div>' + '<div class="panel-sp-l">' + '主讲人：' + each['teacher'] + '</div>' + '<div class="panel-sp-r">' + '<button class="button-enter-style" id="' + each['id'] + '">去上课</button>' + '</div>' + '</div>' + '</div>' + '</div>');
                            $("#main_content").append('<div class="panel panel-default">' + '<div class="panel-body">' + '<div class="panel-p">' +'<div class="panel-fp-l">' + 'Git项目管理工具简介' + '</div>' + '<div class="panel-fp-r">' + '8012人学过' + '</div>' + '<div class="panel-sp-l">' + '主讲人： 徐世杰' + '</div>' + '<div class="panel-sp-r">' + '<button class="button-enter-style" id="' + 1 + '" onclick="forDetail(id)">去上课</button>' + '</div>' + '</div>' + '</div>' + '</div>');
                        }
                        $('#page_course').append('<button class="page_up_down" id="up_page" onclick="page_change(id)">上一页</button>&nbsp;&nbsp;');
                        var temporary_record_page = record_page;//创建一个临时使用的参数，先赋予当前页数的值
                        var show_page = 7;//显示的页面按钮数
                        if(limit - temporary_record_page < 7)
                        {
                            temporary_record_page = limit - 6;//保证按钮一直显示7个
                        }
                        if(limit < 7)
                        {
                            temporary_record_page = 1;
                            show_page = limit;
                        }
                        for(var page = 1 ; page <= show_page; page++ )
                        {
                            if(temporary_record_page == record_page)
                            {
                                $('#page_course').append('<button class="page_press" id="page+' + temporary_record_page + '" onclick="page_change(id)">' + (temporary_record_page++) + '</button>&nbsp;');
                            }
                            else
                            {
                                $('#page_course').append('<button class="page" id="page+' + temporary_record_page + '" onclick="page_change(id)">' + (temporary_record_page++) + '</button>&nbsp;');
                            }
                        }
                        $('#page_course').append('&nbsp;<button class="page_up_down" id="down_page" onclick="page_change(id)">下一页</button>');
                        //<button class="page_up_down">上一页</button><button class="page">1</button><button class="page_up_down">下一页</button>
                    }
                }
            });
        })

function page_change(id)
{
    if( id == "up_page" )
    {
        if(record_page <= 1)
        {
            window.location.href = "courseList.html";
        }
        else
        {
            window.location.href = "courseList.html?page=" + (record_page - 1);
        }
    }
    else if( id == "down_page" )
    {
        if(record_page >= limit)
        {
            window.location.href = "courseList.html?page=" + record_page;
        }
        else
        {
            window.location.href = "courseList.html?page=" + (++record_page);
        }
    }
    else
    {
        window.location.href = "courseList.html?page=" + id.split("+")[1];
    }
}